<?php
namespace app\api\controller;
use think\Controller;
use app\api\logic\Index as lindex;
class Index extends  Base
{
    public function initialize(){
        parent::initialize();
        $this->Logic = new lindex($this->AccountInfo);
        $this->Validate = new \app\api\validate\Index;
    }

    //提交用户信息
    public function send_user_info()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $res = $this->Logic->sendUserInfo($param);
            return $this->print_result($res);
        }
    }

    // 获取用户信息
    public function get_user_info()
    {
        if ($this->request->isPost()) {
            $user = $this->Logic->getUserInfo();
            $this->error_data['ErrorCode'] = 1;
            $this->error_data['ErrorMsg'] = '查询成功';
            $this->error_data['Data'] = $user;
            return $this->print_result($this->error_data);
        }
    }

    // 搜索箱柜编号
    public function search_box()
    {
        if ($this->request->isPost()) {
            $box_number = $this->request->param('box_number');
            if (!$box_number) {
                $this->error_data['ErrorMsg'] = '请输入箱柜编号';
                return $this->print_result($this->error_data);
            }
            $res = $this->Logic->searchBox($box_number);
            if (!$res['ErrorCode']) {
                return $this->print_result($res);
            }
            $this->error_data['ErrorCode'] = 1;
            $this->error_data['ErrorMsg'] = '查询成功';
            $this->error_data['Data'] = $res['Data'];
            return $this->print_result($this->error_data);
        }
    }

    // 我要发货
    public function send_goods()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // 参数验证
            if (!$this->Validate->check($param)) {
                $this->res_data['ErrorMsg'] = $this->Validate->getError();
                return $this->print_result($this->res_data);
            }
            $res = $this->Logic->sendGoods($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }

    // banner
    public function banner()
    {
        if ($this->request->isPost()) {
            $res = $this->Logic->getBanner();
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            $this->res_data['Data'] = $res;
            return $this->print_result($this->res_data);
        }
    }

    // 历史货运
    public function freight_history()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['page'])) $param['page'] = 1;
            if (!isset($param['length'])) $param['length'] = 6;
            $res = $this->Logic->freightHistory($param);
            return $this->print_result($res);
        }
    }

    // 我的发货
    public function freight_order()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['page'])) $param['page'] = 1;
            if (!isset($param['length'])) $param['length'] = 6;
            $res = $this->Logic->freightOrder($param);
            return $this->print_result($res);
        }
    }

    // 我的货柜
    public function my_box()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['status'])) {
                $this->res_data['ErrorMsg'] = '缺少状态参数';
                return $this->print_result($this->res_data);
            }
            if (!isset($param['page'])) $param['page'] = 1;
            if (!isset($param['length'])) $param['length'] = 6;
            if (!isset($param['type'])) {
                $this->res_data['ErrorMsg'] = '缺少类型参数';
                return $this->print_result($this->res_data);
            }
            $res = $this->Logic->myBox($param);
            return $this->print_result($res);
        }
    }

    public function delete_my_box()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (!$id) {
                $this->res_data['ErrorMsg'] = '缺少参数';
                return $this->print_result($this->res_data);
            }
            $res = $this->Logic->deleteMyBox($id);
            return $this->print_result($res);
        }
    }

    // 我的货柜详情
    public function my_box_info()
    {
        if ($this->request->isPost()) {
            $box_number = $this->request->param('box_number');
            if (!$box_number) {
                $this->res_data['ErrorMsg'] = '缺少参数';
                return $this->print_result($this->res_data);
            }
            $res = $this->Logic->myBoxInfo($box_number);
            return $this->print_result($res);
        }
    }

    // 关于我们
    public function about_us()
    {
        if ($this->request->isPost()) {
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            $this->res_data['Data'] = $this->Logic->aboutUs();
            return $this->print_result($this->res_data);
        }
    }

    // 绑定手机号,获取验证码
    public function get_verify_code()
    {
        if ($this->request->isPost()) {
            $phone = $this->request->param('phone');
            if (!$phone) {
                $this->error_data['ErrorMsg'] = '请填写手机号';
                return $this->print_result($this->error_data);
            }
            if (!preg_match('/^1\d{10}$/',$phone)) {
                $this->error_data['ErrorMsg'] = '手机号格式错误';
                return $this->print_result($this->error_data);
            }
            $res = $this->Logic->getVerifyCode($phone);
            return $this->print_result($res);
        }
    }

    // 绑定手机号
    public function bind_phone()
    {
        if ($this->request->isPost()) {
            $phone = $this->request->param('phone');
            $verify = $this->request->param('verify');
            if (!$phone) {
                $this->error_data['ErrorMsg'] = '请填写手机号';
                return $this->print_result($this->error_data);
            }
            if (!$verify) {
                $this->error_data['ErrorMsg'] = '缺少验证码';
                return $this->print_result($this->error_data);
            }
            if (!preg_match('/^1\d{10}$/',$phone)) {
                $this->error_data['ErrorMsg'] = '手机号格式错误';
                return $this->print_result($this->error_data);
            }
            $res = $this->Logic->bindPhone($phone,$verify);
            return $this->print_result($res);
        }
    }

    // 绑定邮箱
    public function bind_mail()
    {
        if ($this->request->isPost()) {
            $mail = $this->request->param('mail');
            if (!$mail) {
                $this->error_data['ErrorMsg'] = '请填写邮箱';
                return $this->print_result($this->error_data);
            }
            if (!filter_var($mail,FILTER_VALIDATE_EMAIL)) {
                $this->error_data['ErrorMsg'] = '邮箱格式错误';
                return $this->print_result($this->error_data);
            }
            $res = $this->Logic->bindMail($mail);
            return $this->print_result($res);
        }
    }

    // 箱柜尺寸
    public function get_box_size()
    {
        if ($this->request->isPost()) {
            $res = $this->Logic->getBoxSize();
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            $this->res_data['Data'] = $res;
            return $this->print_result($this->res_data);
        }
    }

    // 物品类型
    public function get_item_type()
    {
        if ($this->request->isPost()) {
            $res = $this->Logic->getItemType();
            $this->res_data['ErrorMsg'] = 'success';
            $this->res_data['ErrorCode'] = 1;
            $this->res_data['Data'] = $res;
            return $this->print_result($this->res_data);
        }
    }

    // 我的提箱任务
    public function driver_task()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['page'])) $param['page'] = 1;
            if (!isset($param['length'])) $param['length'] = 6;
            $res = $this->Logic->driverTask($param);
            return $this->print_result($res);
        }
    }

    // 提箱任务详情
    public function driver_task_info()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            $res = $this->Logic->driverTaskInfo($id);
            return $this->print_result($res);
        }
    }

    // 货运需求管理
    public function freight_order_task()
    {
        if($this->request->isPost()){
            $param = $this->request->param();
            $res = $this->Logic->freightOrderTask($param);
            return $this->print_result($res);
        }
    }

    // 处理货运需求
    public function change_status()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if (!isset($param['id']) || !isset($param['status'])) {
                $this->res_data['ErrorMsg'] = '缺少必要参数';
                return $this->print_result($this->res_data);
            }
            $param['id'] = intval($param['id']);
            $param['status'] = intval($param['status']);
            $res = $this->Logic->changeStatus($param);
            if ($res['ErrorCode']==0) {
                $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
                return $this->print_result($this->res_data);
            }
            $this->res_data['ErrorMsg'] = $res['ErrorMsg'];
            $this->res_data['ErrorCode'] = 1;
            return $this->print_result($this->res_data);
        }
    }
}
